import importlib
import sys
from docx import Document
from openpyxl import load_workbook
from datetime import datetime
import os.path

importlib.reload(sys)

# 读取excel文件:111.xlsx
dir_path = os.path.join(".", "file")
file_path = os.path.join(dir_path, '111.xlsx')
wb = load_workbook(filename=file_path)
# word文档
document = Document()
# 添加world标题
document.add_heading('从excel读取的数据', 0)
# 要读取多少列
maxCols = 3
# 创建一个0行maxCols列的表格
table = document.add_table(rows=0, cols=maxCols)
# 获取所有表名
sheetnames = wb.sheetnames
# 取第1张表
ws = wb[sheetnames[0]]
# 循环读取excel中每行的内容写入word
for row in ws.rows:
    # world表格中增加一行
    row_cells = table.add_row().cells
    for i in range(maxCols):
        # 将excel中每行第i列的数据取出，赋值给world中每行的第i列
        row_cells[i].text = str(row[i].value)

# 结束world的写入并存储
document.add_page_break()
file_path = os.path.join(dir_path, '111-%s.docx' % datetime.now().strftime('%Y%m%d%H%M%S'))
document.save(file_path)
